package ee.ut.maxorato_macobo.oop.mudclient.network;

import java.io.IOException;
import java.nio.channels.ClosedChannelException;

import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

public class ClientEventHandler extends SimpleChannelUpstreamHandler {
	@Override
	public void handleUpstream(ChannelHandlerContext context, ChannelEvent e) throws Exception {
		super.handleUpstream(context, e);
	}
	
	@Override
	public void channelConnected(ChannelHandlerContext context, ChannelStateEvent event) {
		//ClientIoManager.setChannel(context.getChannel());
	}
	
	@Override
	public void channelDisconnected(ChannelHandlerContext context, ChannelStateEvent event) {
		ClientIoManager.setChannel(null);
		ClientIoManager.gotDisconnected();
	}
	
	@Override
	public void messageReceived(ChannelHandlerContext context, MessageEvent event) {
		ClientIoManager.gotInput((String)event.getMessage());
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext context, ExceptionEvent exception) {
		if(exception.getCause() instanceof ClosedChannelException ||
				exception.getCause() instanceof IOException) {
			
		}
		else {
			exception.getCause().printStackTrace();
		}
	}
}
